/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package repositoriodistribuido;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author ylopez
 */
public class dmonCommandsImpl extends java.rmi.server.UnicastRemoteObject implements dmonCommands {

    private static List<String> ndrep = new ArrayList<String>();;
    private static int[] nrepAndNops = new int[]{0,0,0};
    private static List<String> users = new ArrayList<String>();

    public dmonCommandsImpl() throws java.rmi.RemoteException {
        super();
    }

    public void addUser(String userName, Date time) throws RemoteException {
        users.add(userName + "  " + time.toString());
    }

    public void connectDmon(String drepName, Date time) throws RemoteException {
        ndrep.add("Drep: " +drepName + "  " + time.toString());
        nrepAndNops[0]++;
    }

    public String[] tuser() throws RemoteException {
        Object[] usersArray = users.toArray();
        String[] result = new String[2*usersArray.length];
        String[] tmp;
        for (int i = 0; i < usersArray.length; i++) {
            tmp = (usersArray[i].toString()).split("  ");
            result[2*i] = tmp[0];
            result[(2*i)+1] = tmp[1];
        }
        return result;
    }

    public String[] ndrep() throws RemoteException {
        Object[] drepsArray = ndrep.toArray();
        String[] result = new String[2*drepsArray.length];
        String[] tmp;
        for (int i = 0; i < drepsArray.length; i++) {
            tmp = (drepsArray[i].toString()).split("  ");
            result[2*i] = tmp[0];
            result[(2*i)+1] = tmp[1];
        }
        return result;
    }

    public String[] nops() throws RemoteException {
        String[] result = new String[2];
        result[0] = Integer.toString(nrepAndNops[1]);
        result[1] = Integer.toString(nrepAndNops[2]);
        return result;
    }

    public int nrep() throws RemoteException {
        return nrepAndNops[0];
    }

    public void remove(String user) throws RemoteException {
        Object[] usersArray = users.toArray();
        String tmp;
        for (int i = 0; i < usersArray.length; i++) {
            tmp = usersArray[i].toString();
            if (tmp.equals(user)){
                users.remove(users.get(i));
            }
        }
    }

    public void disconnectDmon(String drepName) throws RemoteException {
        Object[] ndrepArray = ndrep.toArray();
        String tmp;
        for (int i = 0; i < ndrepArray.length; i++) {
            tmp = ndrepArray[i].toString();
            if (tmp.equals("Drep: " + drepName)){
                ndrep.remove(ndrep.get(i));
            }
        }
        nrepAndNops[0]--;
    }

    public static void printResoults(String flag, String[] resoults){
        System.out.println("Inicio ejecuci'on "+ flag +":");
        if (flag.equals("ldir") || flag.equals("rdir") || flag.equals("tuser") || flag.equals("ndrep")) {
            for (int i = 0; i < resoults.length; i += 2) {
                System.out.println(resoults[i] + " " + resoults[i+1]);
            }
        }
        if (flag.equals("nrep")) {
            System.out.println("N'umero de repositorios: " + resoults[0]);
        }

        if (flag.equals("sube") || flag.equals("baja")) {
            System.out.println("Estatus de sube: " + resoults[0]);
        }

        if (flag.equals("nops")) {
            System.out.println("N'umero operaciones de sube: " + resoults[0]);
            System.out.println("N'umero operaciones de baja: " + resoults[1]);
        }
        System.out.println("Final ejecuci'on "+ flag +".");
    }

    public void download() throws RemoteException {
        nrepAndNops[2]++;
    }

    public void upload() throws RemoteException {
        nrepAndNops[1]++;
    }

}
